package com.hnkywl.sts.dao.zjgl;

import com.hnkywl.sts.dao.base.RegionDao;
import com.hnkywl.sts.entity.djgl.Jydd;
import com.hnkywl.sts.entity.zjgl.Hthk;
import com.hnkywl.sts.entity.zjgl.Jzjs;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by HL on 2016/4/12.
 */
@Repository
public class JzjsDao extends SimpleHibernateTemplate<Jzjs, Long> {

    public List<Jzjs> checkHtJzjs(Long cghtId) {
        return this.find("from Jzjs where  cght.id = ? and zt = '0' order by dateCreated", cghtId);
    }


    public Page<Jzjs> findJzjsList(Long gsId, String khMc, String hth, int pageNo, int pageSize) {
        String hql;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Jzjs> page = new Page<Jzjs>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (gsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(gsId);
        }
        if (khMc != null && !("").equals(khMc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" kh.qc like ?");
            param.add("%" + khMc.trim() + "%");
        }
        if (hth != null && !("").equals(hth)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" cght.htbm like ?");
            param.add("%" + hth.trim() + "%");
        }
        //==============记录数量===============
        hql = "select count(*) from Jzjs where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Jzjs where 1=1" + RegionDao.createDataRegion("and");
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        hql = hql + " order by zt, dateCreated desc ";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }


}
